<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%@ include file="/WEB-INF/views/common.jsp"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<script type="text/javascript">
	$(function(){
		var uploadForm = $("#uploadForm");
		var downLoadForm = $("#downLoadForm");
		var questionDatagrid = $("#questionDatagrid");
		var cmdObject = {
				import : function() {
					uploadForm.form('submit',{
						url : "/question/upload",
						//后台加验证，防止被脚本导入错误文件
						success : function(data) {
							var data = JSON.parse(data);
							if(data.code == 0) {
								$.messager.alert('提示', data.msg, 'info');
								var file = $("#file") 
								file.after(file.clone().val("")); 
								file.remove();
								//重新刷新departmentDatagrid
								questionDatagrid.datagrid('reload');
							} else {
								$.messager.alert('提示', data.msg, 'error');
							}
						}
					})
				},
				deleteAll : function() {
					$.messager.confirm('提示', '确定要清空题库？', function(r) {
						if (r) {
							$.get("/question/deleteAll", function(data) {
								if (data.code == 0) {
									//重新刷新Datagrid
									$.messager.alert('提示', data.msg, 'info');
									questionDatagrid.datagrid('reload');
								} else {
									$.messager.alert('提示', data.msg, 'error');
								}
							});
						}
					})
				},
				update : function() {
					var row = questionDatagrid.datagrid('getSelected');
					if (!row) {
						$.messager.alert('提示', '请选中要更改的一行', 'info');
						return;
					}
					$.messager.confirm('提示', '要更改该试题的状态吗？', function(r) {
						if (r) {
							$.get("/question/updateSteate?questionState=" + row.state+"&id="+row.id, function(data) {
								if (data.code == 0) {
									//重新刷新Datagrid
									questionDatagrid.datagrid('reload');
								} else {
									$.messager.alert('提示', data.msg, 'error');
								}
							});
						}
					})
				}
				
			}
			$("a[data-cmd]").on('click', function() {
				var cmd = $(this).data("cmd");
				if (cmd && !$(this).hasClass('l-btn-disabled')) {
					cmdObject[cmd]();
				}
			});
	})
	function stateFormat(value, row, index) {　/* 标准格式，应使用枚举 */
		var result = '';
		if (value == 0) {
			result = '正 常';
		} else {
			result = '<font color="red">禁 用</font>';
		}
		return result;
	}
	
	function typeFormat(value, row, index) {
		var result = '';
		if(value == 1) {
			result = '单选';
		} else if(value == 2){
			result = '多选';
		} else {
			result = '判断';
		}
		return result;
	}
	
	function timeFormat(value, row, index) {
		var date = new Date(value);
		return date.format("yyyy-MM-dd hh:mm:ss");
	}
	/** 
	 *时间格式转换，copy */
	Date.prototype.format = function(format) {
		var o = {
			"M+" : this.getMonth() + 1, //month 
			"d+" : this.getDate(), //day 
			"h+" : this.getHours(), //hour 
			"m+" : this.getMinutes(), //minute 
			"s+" : this.getSeconds(), //second 
			"q+" : Math.floor((this.getMonth() + 3) / 3), //quarter 
			"S" : this.getMilliseconds()
		//millisecond 
		}
		if (/(y+)/.test(format)) {
			format = format.replace(RegExp.$1, (this.getFullYear() + "")
					.substr(4 - RegExp.$1.length));
		}
		for ( var k in o) {
			if (new RegExp("(" + k + ")").test(format)) {
				format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
						: ("00" + o[k]).substr(("" + o[k]).length));
			}
		}
		return format;
	}
</script>
<body>
<table id="questionDatagrid" class="easyui-datagrid" toolbar="#studentTooBar"
		url="/question/list" pagination="true" fit="true" fitColumns="true"
		singleSelect="true" striped="true">
		<thead>
			<tr>
				<th field="intro" width="100">&nbsp;题干</th>
				<th field="optionA" width="50">&nbsp;A选项</th>
				<th field="optionB" width="50">&nbsp;B选项</th>
				<th field="optionC" width="50">&nbsp;C选项</th>
				<th field="optionD" width="50">&nbsp;D选项</th>
				<th field="result" align="center" width="30">答案</th>
				<th field="createTime" align="center" width="50" formatter="timeFormat">创建时间</th>
				<th field="type" align="center" width="30" formatter="typeFormat">试题类型</th>
				<th field="state" align="center" width="30" formatter="stateFormat">状态</th>
			</tr>
		</thead>
	</table>
	<div id="studentTooBar">
		<form id="uploadForm" enctype="multipart/form-data" method="post">
			<input type="file" class="easyui-filebox" id="file" 
				name="file" style="width:160px;">
			<a id="import"
				href="javascript:void(0)" class="easyui-linkbutton"
				iconCls="icon-add" data-cmd="import">导入</a>
			<a id="delete"
				href="javascript:void(0)" class="easyui-linkbutton"
				iconCls="icon-remove" data-cmd="deleteAll">清空试题</a>
			&nbsp;&nbsp;&nbsp;
			<a id="update"
				href="javascript:void(0)" class="easyui-linkbutton"
				iconCls="icon-remove" data-cmd="update">启用/禁用（题）</a>
		</form>
	</div>
		
	
</body>
</html>